package com.example.util;/**
 * @author wangXiao
 */

import com.example.entity.CmdTask;
import com.example.entity.ReadMeConfig;
import com.example.entity.Task;
import com.example.entity.TaskConfig;
import com.google.common.collect.ImmutableMap;
import org.yaml.snakeyaml.Yaml;

import javax.validation.ConstraintViolation;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.*;

/**
 * @Description: TODO
 * @Author wangXiao
 * @Date 2022/8/12 
 **/
public class ConfigParser {
    private final static ImmutableMap<String, Class<? extends Task>> TASK_MAP;

    static {
        ImmutableMap.Builder<String, Class<? extends Task>> builder = ImmutableMap.builder();

        Enumeration<URL> urls;
        try {
            urls = Thread.currentThread().getContextClassLoader().getResources("META-INF/version-config.yml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            try {
                Map<String, Object> config = (Map<String, Object>) (new Yaml().load(url.openStream()));
                List<String> tasks = (List<String>) config.get("tasks");
                for (String task : tasks) {
                    Class<?> cls = Class.forName(task);
                    if (!Task.class.isAssignableFrom(cls)) {
                        continue;
                    }
                    Class<? extends Task> taskClass = (Class<? extends Task>) cls;
                    //TaskType taskType = taskClass.getAnnotation(TaskType.class);
                   /* if (taskType != null) {
                        builder.put(taskType.value(), taskClass);
                    } else {
                        builder.put(taskClass.getSimpleName(), taskClass);
                    }*/
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }


        TASK_MAP = builder.build();
    }

    public static ReadMeConfig parse(File configFile) {
        ReadMeConfig readMeConfig;
        try (FileInputStream inputStream = new FileInputStream(configFile)) {
            readMeConfig = new Yaml().loadAs(inputStream, ReadMeConfig.class);
        } catch (Exception e) {
            throw new RuntimeException(String.format("配置文件解析错误: %s ", e.getMessage()));
        }

        // 检查配置文件参数
        Set<ConstraintViolation<ReadMeConfig>> violations = ValidatorUtils.validate(readMeConfig);
        if (!violations.isEmpty()) {
            ConstraintViolation<ReadMeConfig> violation = violations.stream().findFirst().get();
            throw new RuntimeException(String.format("配置文件解析错误: %s %s", violation.getPropertyPath(), violation.getMessage()));
        }

        if (readMeConfig.getTasks() != null && readMeConfig.getTasks().size() > 0) {
            readMeConfig.setTaskInstances(new ArrayList<>());
            for (TaskConfig taskConfig : readMeConfig.getTasks()) {
                readMeConfig.getTaskInstances().add(parseTask(taskConfig, readMeConfig));
            }
        }

        //
        /*if (readMeConfig.getTasks() != null && readMeConfig.getTasks().size() > 0) {
            readMeConfig.setTaskInstances(new ArrayList<>());
            for (TaskConfig taskConfig : readMeConfig.getTasks()) {
                readMeConfig.getTaskInstances().add(parseTask(taskConfig, readMeConfig));
            }
        }*/

        return readMeConfig;
    }

    private static Task parseTask(TaskConfig taskConfig, ReadMeConfig readMeConfig) {

        try {
            Task task =  new CmdTask();

            return task;
        } catch (Exception e) {
            throw new RuntimeException(String.format("配置文件解析错误: %s ", e.getMessage()));
        }
    }
}
